//var mongodb = require('./db');
var db = require('./db');
var mongodb = new db();
//var ObjectID = require("mongodb").ObjectID;
var ccdIndex = require('../models/ccdIndex.js');
//基本实现

exports.create= function createCD(filter,callback){
    var cd = {
        user : filter.user,
        ccd_id : filter.ccd_id
    }
    mongodb.getCollection('ccd',function(collection){
        var findFilter = {};
        for(var cd_name in filter["cd"]){
            var cd="cd."+cd_name+"._nor";
            findFilter[cd] = 1;
            break;
        }
        //console.log("...............")
        //console.log(findFilter);
        collection.findOne(findFilter, function(err, doc) {
            console.log(err);
            console.log(doc);
            if(err != null){
                console.log(err);
                return callback(err, doc);
            }
            else if(doc != null){
                err = "already Exist";
                return callback(err, doc);
            }
            else{
                collection.insert(filter, {safe: true}, function(err, doc) {
                    ccdIndex.create(doc[0],function(err2,doc2){
                        return callback(err, doc);
                    });
                });
            }
        });
    });
};

//get正常
exports.get = function getCD(filter,callback){
    mongodb.getCollection('ccd',function(collection){
        collection.findOne(filter, function(err, doc) {
             if(err != null){
                return callback(err, doc);
             }
             if(doc === null){
                 err = "not Exist";
                 return callback(err, doc);
             }
             return callback(err, doc);
        });
    });
};

//测试通过
exports.remove = function removeIcd(filter,callback){
    mongodb.getCollection('ccd',function(collection){
        collection.remove(filter[0],function(err){
            if (err) {
                console.warn(err.message);
                return callback();
            }
            else {
                console.log('ccd successfully removed');
                ccdIndex.remove(filter[0],function(){
                    return callback();
                });
            }
        });
    });
};

exports.add= function addElem(filter,callback){
    var x = filter[0];
    var y ={};

    y[filter[1]]=1;
    console.log("ccd add ");
    console.log(y);

    mongodb.getCollection('ccd',function(collection){
       collection.update( x,{"$inc":y},{safe:true},function(err,number) {
        if (err) console.warn(err.message);
            else console.log('ccd successfully updated');
            callback(err,number);
        });
    });
};

exports.add2= function addElem2(filter,callback){
    var x = filter[0];
    var y ={};
    y[filter[1]]=1;
    y[filter[2]]=1;
    mongodb.getCollection('ccd',function(collection){
        collection.update( x,{"$inc":y},{safe:true},function(err,number) {
            if (err) console.warn(err.message);
            else console.log('ccd successfully updated');
            //console.log(number); //多个添加仍为1
            callback(err,number);
        });
    });
};
//
exports.ref= function refElem(filter,subFilter,callback){
    mongodb.getCollection('ccd',function(collection){
        collection.update(filter,{"$inc":subFilter},{safe:true},function(err,number) {
            if (err) console.warn(err.message);
            else console.log('ccd successfully deleted');
            console.log(number)
            return callback(err,number);
        });
    });
};

exports.generateSubFilter = function generateSubFilter(sub,callback){
    var subFilter=[];
    var preFilter=".";
    generateFilter(sub,preFilter,subFilter);
    return callback(subFilter);
};

generateFilter = function(sub,preFilter,subFilter){
    for(var key in sub){
        if(key === '_nor'){
            subFilter[subFilter.length] = preFilter+key
        }
        else{
            generateFilter(sub[key],preFilter+key+".",subFilter);
        }
    }
}